package com.chengyu.core.util.file;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * @ Author     ：mmzs.
 * @ Date       ：Created in 23:51 2024/1/12
 * @ Description：文件工具
 * @ Modified By：
 * @Version: 1.0.0$
 */
public class FileUtil {
    // 第二种方式
    public static byte[] file2ByteArray (String url) {
        byte[] data = new byte[0];
        try (InputStream inputStream = new URL(url).openStream()) {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

            int bytesRead;
            byte[] buffer = new byte[4096];
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            data = outputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;

    }
}
